<template>
  <div>
    <el-dialog title="新增" :model-value="addDialogVisible" @open="handleOpen" :before-close="handleCancel" width="30%">
      <el-form :model="formData" :rules="rules" label-width="100px" ref="addForm">
        <el-form-item label="资源名:" prop="resourceName">
          <el-input v-model="formData.resource.resourceName"></el-input>
        </el-form-item>
        <el-form-item label="资源描述:" prop="resourceDescription">
          <el-input v-model="formData.resource.resourceDescription"></el-input>
        </el-form-item>
        <el-form-item label="标签:" prop="tag">
          <el-input v-model="formData.resource.tag"></el-input>
        </el-form-item>
        <el-form-item label="资源类型:" prop="resourceType">
          <el-select v-model="formData.resource.resourceType" placeholder="请选择资源类型">
            <el-option label="视频" value="video"></el-option>
            <el-option label="图片" value="image"></el-option>
            <el-option label="文本" value="text"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传资源文件:" prop="file">
          <el-upload
              ref="fileUpload"
              action="#"
              :auto-upload="false"
              :on-change="handleFileChange"
              :limit="1"
              :file-list="fileList"
          >
            <el-button slot="trigger" type="primary">选择文件</el-button>
            <div slot="tip" class="el-upload__tip">只能上传一个文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="上传资源封面:" prop="coverFile">
          <el-upload
              ref="coverFileUpload"
              action="#"
              :auto-upload="false"
              :on-change="handleCoverFileChange"
              :limit="1"
              :file-list="coverFileList"
          >
            <el-button slot="trigger" type="primary">选择文件</el-button>
            <div slot="tip" class="el-upload__tip">只能上传一个文件</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name='AddAuthority' lang='ts'>
import {reactive, ref} from 'vue';
import {ElMessage} from 'element-plus';
import {cosApi, resourceApi} from '@/api/resourceApi';
import {useUserStore} from "@/store/useUserStore";
const userStore = useUserStore();
// 新增权限对应的字段
let formData = reactive({
  resource: {
    resourceType: '',
    resourceDescription: '',
    userId: '',
    resourceName: '',
    coverName: '',
    tag: ''
  },
  file: null as File | null,
  coverFile: null as File | null,
});

let rules = reactive({
  resource: {
    resourceName: [
      { required: true, message: "请输入资源名称", trigger: "blur" },
    ],
    resourceDescription: [
      { required: true, message: "请输入资源描述", trigger: "blur" },
    ],
    tag: [
      { required: true, message: "请输入标签", trigger: "blur" },
    ],
    resourceType: [
      { required: true, message: "请输入资源类型", trigger: "blur" },
    ],
    userId: [
      { required: true, message: "请输入用户id", trigger: "blur" },
    ]
  },
  file: [
    { required: true, message: "请上传资源文件", trigger: "change" },
  ],
  coverFile: [
    { required: true, message: "请上传资源封面文件", trigger: "change" },
  ]
});

let addForm = ref();
let fileUpload = ref();
let coverFileUpload = ref();
let fileList = ref([]);
let coverFileList = ref([]);

defineProps<{ addDialogVisible: boolean }>();
let emitter = defineEmits(["closeDialog", "addSuccess"]);

function handleOpen() {
  // 打开对话框事件
}

function handleCancel() {
  addForm.value.resetFields();
  fileUpload.value.clearFiles();
  coverFileUpload.value.clearFiles();
  emitter("closeDialog");
}

function handleFileChange(file: any) {
  formData.file = file.raw;
}

function handleCoverFileChange(file: any) {
  formData.coverFile = file.raw;
}





async function onSubmit() {
  const file = formData.file;
  const coverFile = formData.coverFile;
  if(coverFile){
    formData.resource.coverName = formData.resource.resourceName + '_cover';
  }
  if (!file) {
    ElMessage.error('请上传资源文件');
    return;
  }
  if(!formData.resource.resourceName){
    ElMessage.error('请输入资源名称');
    return;
  }
  if (!formData.resource.resourceType) {
    ElMessage.error('请输入资源类型');
    return;
  }
  formData.resource.userId = userStore.userInfo.id;

  try {
    // 上传资源文件（断点续传）
    await cosApi.uploadLargeFile(formData.resource.resourceName, file);
    // 上传封面文件
    if (coverFile){
      await cosApi.uploadLargeFile(formData.resource.coverName, coverFile)
    }
    // 新增资源
    await resourceApi.saveResource(formData.resource)
    ElMessage.success('上传成功');
    handleCancel();
  } catch (error) {
    ElMessage.error('上传失败，请重试');
    console.error(error);
  }
}



</script>

<style scoped></style>